package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.common.constant.CommonConstant;
import cn.darkhorse.common.constant.WebSocketEnum;
import cn.darkhorse.device.dto.MesRepairInfoDTO;
import cn.darkhorse.device.pojo.MesRepairInfo;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.device.service.MesRepairInfoNewService;
import cn.darkhorse.device.vo.MesRepairApplyInfoVO;
import cn.darkhorse.device.vo.MesRepairVO;
import cn.darkhorse.meswechat.service.IWeChatService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 报修单 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2022-11-11
 */
@RestController
@RequestMapping("/device/mesRepairInfoRepair")
public class MesRepairInfoRepairController extends BaseController {
   @Resource
   private MesRepairInfoNewService mesRepairInfoService;
   @Resource
   private ISysDictDataService dictDataService;
    @Autowired
    private MesDeviceService mesDeviceService;

    @Autowired
    private IWeChatService weChatService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 列表
     * @param mesRepairInfoDTO
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public TableDataInfo list( MesRepairInfoDTO mesRepairInfoDTO){
        startPage();
        mesRepairInfoDTO.setDocumentType(1);
        List<MesRepairApplyInfoVO> mesRepairInfoVOS = mesRepairInfoService.listAll(mesRepairInfoDTO);
        return  getDataTable(mesRepairInfoVOS);
    }


    /**
     * 列表
     * @param mesRepairInfoDTO
     * @return
     */
    @RequestMapping(value = "/listAllRepairInfo",method = RequestMethod.POST)
    public TableDataInfo listAllRepairInfo( MesRepairInfoDTO mesRepairInfoDTO){
        startPage();
        return  getDataTable(mesRepairInfoService.listAllRepairInfo(mesRepairInfoDTO));
    }
    /**
     * 维修单导出
     *
     * @param mesRepairInfoDTO
     * @return
     */
    @RequestMapping(value = "/export",method = RequestMethod.POST)
    public AjaxResult export(MesRepairInfoDTO mesRepairInfoDTO){
        mesRepairInfoDTO.setDocumentType(1);
        List<MesRepairApplyInfoVO> mesRepairInfoVOS = mesRepairInfoService.listAll(mesRepairInfoDTO);
        ExcelUtil<MesRepairApplyInfoVO> util = new ExcelUtil<>(MesRepairApplyInfoVO.class);
        return util.exportExcel(mesRepairInfoVOS, "维修工单","维修工单");
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.POST)
    public AjaxResult view(Long id){
        if(null==id){
            return  AjaxResult.error("缺失参数！");
        }
        MesRepairApplyInfoVO mesRepairInfoVO = mesRepairInfoService.view(id);
        return AjaxResult.success(mesRepairInfoVO);
    }

    /**
     * 生成报修维修单号
     * @param type 0保修单1维修单
     * @return
     */
    @RequestMapping("/getApplyCode")
    public AjaxResult getApplyCode(Integer type){
        String code=mesRepairInfoService.getApplyCode(type);
        return AjaxResult.success(code);
    }
    /**
     * 保存
     * @param mesRepairInfo
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public AjaxResult save(@Validated @RequestBody MesRepairInfo mesRepairInfo){
        if(null==mesRepairInfo){
            return  AjaxResult.error("缺失参数！");
        }

        return AjaxResult.success(mesRepairInfoService.saveMesRepairInfo(mesRepairInfo));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(@RequestParam("ids") List<Long> ids){
        if(ids.isEmpty()){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.delete(ids);
        return AjaxResult.success(result);
    }


    /**
     *派工
     * @param id
     * @param teamName 班组名称
     * @param repairPersons 维修人
     * @param mainRepairers 主修人
     * @return
     */

    @RequestMapping(value = "/dispatch",method = RequestMethod.POST)
    public AjaxResult dispatch(Long id,String teamName,String repairPersons,Long mainRepairers){
        if(null==id|| StringUtils.isBlank(teamName)||StringUtils.isBlank(repairPersons)){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.dispatch(id,teamName,repairPersons, mainRepairers);

        return AjaxResult.success(result);
    }
    /**
     *开始维修
     * @param id
     * @return
     */

    @RequestMapping(value = "/start-repair",method = RequestMethod.POST)
    public AjaxResult startRepair(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.startRepair(id);
        return AjaxResult.success(result);
    }

    /**
     *维修中数据更新
     * @param mesRepairVO
     * @return
     */

    @RequestMapping(value = "/repair",method = RequestMethod.POST)
    public AjaxResult repair(@RequestBody @Validated MesRepairVO mesRepairVO){
        boolean result= mesRepairInfoService.repair(mesRepairVO);
        return AjaxResult.success(result);
    }
    /**
     *开始维修
     * @param id
     * @return
     */

    @RequestMapping(value = "/end-repair",method = RequestMethod.POST)
    public AjaxResult endRepair(Long id){
        if(null==id){
            return AjaxResult.error("缺失参数！");
        }
        boolean result= mesRepairInfoService.endRepair(id);
        return AjaxResult.success(result);
    }

    /**
     *维修中数据更新
     * @param mesRepairVO
     * @return
     */

    @RequestMapping(value = "/verify",method = RequestMethod.POST)
    public AjaxResult verify(@RequestBody @Validated MesRepairVO mesRepairVO){
        boolean result= mesRepairInfoService.verify(mesRepairVO);
        return AjaxResult.success(result);
    }

    /**
     *快速工单
     * @param mesRepairInfo 维修单信息
     * @return
     */

    @RequestMapping(value = "/quick-repair",method = RequestMethod.POST)
    public AjaxResult quickRepair(@Validated @RequestBody MesRepairInfo mesRepairInfo){
        return AjaxResult.success(mesRepairInfoService.quickRepair(mesRepairInfo));
    }
}

